#
# fusion.discovery.opendht
#
# Copyright 2007 Helsinki Institute for Information Technology
# and the authors.
#
# Authors: Toni Ruottu <toni.ruottu@iki.fi>
#          Ken Rimey <rimey@hiit.fi>
#

# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation files
# (the "Software"), to deal in the Software without restriction,
# including without limitation the rights to use, copy, modify, merge,
# publish, distribute, sublicense, and/or sell copies of the Software,
# and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

import hashlib
from xmlrpclib import ServerProxy, Binary

url = 'http://opendht.nyuld.net:5851/'
application = 'P2P-Fusion peer discovery'

def put(key, value, ttl):
    key = hashlib.sha1(key).digest()
    proxy = ServerProxy(url)
    return proxy.put(Binary(key),
                     Binary(value),
                     int(ttl),
                     application)

def get(key, maxvals=2**31-1):
    key = hashlib.sha1(key).digest()
    placemark = Binary('')
    proxy = ServerProxy(url)
    values = []
    while True:
        batch, placemark = proxy.get(Binary(key),
                                     int(maxvals),
                                     placemark,
                                     application)
        values.extend(batch)
        if not placemark.data:
            break

    return [v.data for v in values]

def get_details(key, maxvals=2**31-1):
    key = hashlib.sha1(key).digest()
    placemark = Binary('')
    proxy = ServerProxy(url)
    values = []
    while True:
        batch, placemark = proxy.get_details(Binary(key),
                                             int(maxvals),
                                             placemark,
                                             application)
        values.extend(batch)
        if not placemark.data:
            break

    return [(value.data, ttl, type, secret.data)
            for value, ttl, type, secret in values]

def get_ttl(key, value):
    result = 0
    for v, ttl, type, secret in get_details(key):
        if v == value:
            result = max(result, ttl)
    return result

def refresh(key, value, ttl, minttl):
    remaining = get_ttl(key, value)
    if remaining < minttl:
        return put(key, value, ttl)
    else:
        return None             # Success with nothing to do.
